home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap11 / MyWord / StyleBar.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  5.2 KB  |  179 lines

  1. //***********************************************************************
  2. //
  3. //  StyleBar.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include <afxext.h>
  9. #include <afxcmn.h>
  10. #include <afxrich.h>
  11. #include <stdlib.h>
  12. #include "Resource.h"
  13. #include "MyWordView.h"
  14. #include "StyleBar.h"
  15.  
  16. BEGIN_MESSAGE_MAP (CStyleBar, CToolBar)
  17.     ON_WM_CREATE ()
  18.     ON_CBN_SELENDOK (IDC_FONTNAMES, OnSelectFont)
  19.     ON_CBN_SELENDOK (IDC_FONTSIZES, OnSelectSize)
  20.     ON_CBN_CLOSEUP (IDC_FONTNAMES, OnCloseUp)
  21.     ON_CBN_CLOSEUP (IDC_FONTSIZES, OnCloseUp)
  22. END_MESSAGE_MAP ()
  23.  
  24. int CStyleBar::OnCreate (LPCREATESTRUCT lpcs)
  25. {
  26.     static int nFontSizes[] = {
  27.         8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 32, 36, 48, 72
  28.     };
  29.  
  30.     if (CToolBar::OnCreate (lpcs) == -1)
  31.         return -1;
  32.  
  33.     // Load the toolbar
  34.     if (!LoadToolBar (IDR_STYLE_BAR))
  35.         return -1;
  36.  
  37.     // Create an 8-point MS Sans Serif font for the combo boxes
  38.     CClientDC dc (this);
  39.     int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
  40.  
  41.     m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
  42.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  43.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  44.  
  45.     CFont* pOldFont = dc.SelectObject (&m_font);
  46.  
  47.     TEXTMETRIC tm;
  48.     dc.GetTextMetrics (&tm);
  49.     int cxChar = tm.tmAveCharWidth;
  50.     int cyChar = tm.tmHeight + tm.tmExternalLeading;
  51.  
  52.     dc.SelectObject (pOldFont);
  53.  
  54.     // Add the font name combo box to the toolbar
  55.     SetButtonInfo (8, IDC_FONTNAMES, TBBS_SEPARATOR, cxChar * 32);
  56.  
  57.     CRect rect;
  58.     GetItemRect (8, &rect);
  59.     rect.bottom = rect.top + (cyChar * 16);
  60.  
  61.     if (!m_ctlNameComboBox.Create (WS_CHILD | WS_VISIBLE | WS_VSCROLL |
  62.         CBS_DROPDOWNLIST | CBS_SORT, rect, this, IDC_FONTNAMES))
  63.         return -1;
  64.  
  65.     m_ctlNameComboBox.SetFont (&m_font);
  66.     FillNameComboBox (&dc);
  67.  
  68.     // Add the font size combo box to the toolbar
  69.     SetButtonInfo (10, IDC_FONTSIZES, TBBS_SEPARATOR, cxChar * 12);
  70.  
  71.     GetItemRect (10, &rect);
  72.     rect.bottom = rect.top + (cyChar * 14);
  73.  
  74.     if (!m_ctlSizeComboBox.Create (WS_CHILD | WS_VISIBLE | WS_VSCROLL |
  75.         CBS_DROPDOWNLIST, rect, this, IDC_FONTSIZES))
  76.         return -1;
  77.  
  78.     m_ctlSizeComboBox.SetFont (&m_font);
  79.  
  80.     CString string;
  81.     for (int i=0; i<17; i++) {
  82.         string.Format ("%d", nFontSizes[i]);            
  83.         m_ctlSizeComboBox.AddString (string);
  84.     }
  85.     return 0;
  86. }
  87.  
  88. void CStyleBar::OnSelectFont ()
  89. {
  90.     char szFaceName[LF_FACESIZE];
  91.     int nIndex = m_ctlNameComboBox.GetCurSel ();
  92.     m_ctlNameComboBox.GetLBText (nIndex, szFaceName);
  93.  
  94.     CWordView* pView =
  95.         (CWordView*) ((CFrameWnd*) AfxGetMainWnd ())->GetActiveView ();
  96.     pView->ChangeFont (szFaceName);
  97. }
  98.  
  99. void CStyleBar::OnSelectSize ()
  100. {
  101.     char szSize[8];
  102.     int nIndex = m_ctlSizeComboBox.GetCurSel ();
  103.     m_ctlSizeComboBox.GetLBText (nIndex, szSize);
  104.  
  105.     int nSize = atoi (szSize) * 20; // Need twips
  106.  
  107.     CWordView* pView =
  108.         (CWordView*) ((CFrameWnd*) AfxGetMainWnd ())->GetActiveView ();
  109.     pView->ChangeFontSize (nSize);
  110. }
  111.  
  112. void CStyleBar::OnCloseUp ()
  113. {
  114.     ((CFrameWnd*) AfxGetMainWnd ())->GetActiveView ()->SetFocus ();
  115. }
  116.  
  117. void CStyleBar::FillNameComboBox (CDC* pDC)
  118. {
  119.     ::EnumFontFamilies (pDC->m_hDC, NULL,
  120.         (FONTENUMPROC) EnumFontNameProc, (LPARAM) this);
  121. }
  122.  
  123. int CALLBACK CStyleBar::EnumFontNameProc (ENUMLOGFONT* lpelf,
  124.     NEWTEXTMETRIC* lpntm, int nFontType, LPARAM lParam)
  125. {
  126.     CStyleBar* pWnd = (CStyleBar*) lParam;
  127.     if (nFontType & TRUETYPE_FONTTYPE)
  128.         pWnd->m_ctlNameComboBox.AddString (lpelf->elfLogFont.lfFaceName);
  129.     return 1;
  130. }
  131.  
  132. void CStyleBar::OnUpdateCmdUI (CFrameWnd* pTarget,
  133.     BOOL bDisableIfNoHndler)
  134. {
  135.     CToolBar::OnUpdateCmdUI (pTarget, bDisableIfNoHndler);
  136.  
  137.     CWnd* pWnd = GetFocus ();
  138.     if ((pWnd == &m_ctlNameComboBox) || (pWnd == &m_ctlSizeComboBox))
  139.         return;
  140.  
  141.     // Get the font name and size
  142.     int nTwips;
  143.     char szFaceName[LF_FACESIZE];
  144.  
  145.     CWordView* pView =
  146.         (CWordView*) ((CFrameWnd*) AfxGetMainWnd ())->GetActiveView ();
  147.     pView->GetFontInfo (szFaceName, nTwips);
  148.  
  149.     // Update the font name combo box
  150.     char szSelection[LF_FACESIZE];
  151.     m_ctlNameComboBox.GetWindowText (szSelection, sizeof (szSelection));
  152.  
  153.     if (::lstrcmp (szFaceName, szSelection) != 0) {
  154.         if (szFaceName[0] == 0)
  155.             m_ctlNameComboBox.SetCurSel (-1);
  156.         else {
  157.             if (m_ctlNameComboBox.SelectString (-1, szFaceName) == CB_ERR)
  158.                 m_ctlNameComboBox.SetCurSel (-1);
  159.         }
  160.     }
  161.  
  162.     // Update the font size combo box
  163.     char szSize[4];
  164.     m_ctlSizeComboBox.GetWindowText (szSize, sizeof (szSize));
  165.     int nSizeFromComboBox = atoi (szSize);
  166.     int nSizeFromView = nTwips / 20;
  167.  
  168.     if (nSizeFromComboBox != nSizeFromView) {
  169.         if (nTwips == -1)
  170.             m_ctlSizeComboBox.SetCurSel (-1);
  171.         else {
  172.             CString string;
  173.             string.Format ("%d", nSizeFromView);
  174.             if (m_ctlSizeComboBox.SelectString (-1, string) == CB_ERR)
  175.                 m_ctlSizeComboBox.SetCurSel (-1);
  176.         }
  177.     }
  178. }
  179.